package com.ht.dsp;

import com.ht.aems.dbservice.ConnectionFactory;
import com.ht.aems.fesmodel.FesPersistenceFactory;
import com.ht.aems.ntp.CIMDataUpdator;
import com.ht.aems.ntp.SystemModel;
import com.ht.aems.ntp.impl.MeasValueProvider_nari;
import com.ht.dsp.impl.RemoteDspFacade;
import com.ht.dsp.impl.ScadaDataUpdator_nari;
import com.ht.log.LoggerUtil;
import com.ht.memcached.MemCachedConfigurator;
import hhh.remoteService.utils.ServiceRegister;
import hhh.remoteService.utils.rmi.RmiRegistry;
import hhh.utils.FileUtil;
import hhh.utils.LogUtil;
import rtdb.RtDataHandler_MemCached;
import rtdb.RtMeasureHandler_MemoCached;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * This main method is used for shanghai aems with nari's open3000 system
 *
 * @author Dong Shufeng
 *         Date: 2011-3-22
 */
public class Main4Open3000 {
    public static void main(String[] args) throws IOException, InterruptedException {

        LogUtil.initialLog4j("dsp/dsp.log");
        RmiRegistry.setSpecifiedSocket();

        String f = FileUtil.getRootDirectory() + "/_config/memcache.properties";
        System.out.println("Load memcached config file : " + f);
        MemCachedConfigurator.config(f);

        f = FileUtil.getRootDirectory() + "/_config/dba.properties";
        System.out.println("Load database access config file : " + f);
        ConnectionFactory.setConfig(new FileInputStream(f));
        FesPersistenceFactory.setDatabase("shaems");
        FesPersistenceFactory.setType("sql");
        MeasValueProvider_nari measProvider = new MeasValueProvider_nari();
        measProvider.setMeasId2Analog(DspSysModel.INSTANCE.getScadaAnalog());
        measProvider.setMeasId2Discrete(DspSysModel.INSTANCE.getScadaDiscrete());

        SystemModel.INSTANCE.setMeasProvider(measProvider);
        DspSysModel.INSTANCE.setRtDataHandler(new RtDataHandler_MemCached());
        RtMeasureHandler_MemoCached rtMeasureHandler = new RtMeasureHandler_MemoCached();
        DspSysModel.INSTANCE.setRtMeasureQuery(rtMeasureHandler);
        DspSysModel.INSTANCE.setRtMeasureSaver(rtMeasureHandler);

        RemoteDspFacade.instance.setScadaUpdator(new ScadaDataUpdator_nari());
        CIMDataUpdator updator = new CIMDataUpdator(FileUtil.getRootDirectory() + "/datastore/cim");
        updator.setListener(new DspCimListener(updator));
        RemoteDspFacade.instance.setCimUpdator(updator);
        updator.checkFile();
        ServiceRegister.registerService(DspFacade.class, RemoteDspFacade.instance, "dsp");

        LoggerUtil.initialBufferSize(500);

    }
}
